package com.wxc.thread;

import com.wxc.common.File;
import com.wxc.common.User;
import com.wxc.util.ConnectionUtil;
import com.wxc.util.SocketUtil;
import com.wxc.util.StreamUtil;
import com.wxc.vo.Message;
import com.wxc.vo.Resp;
import com.wxc.vo.RespCode;

import java.net.Socket;
import java.util.UUID;
import java.util.stream.Stream;

/**
 * @author wxc
 * @date 2021年10月04日 21:41
 */
public class ReceiveThread extends Thread {
    private final ConnectionUtil connectionUtil = new ConnectionUtil();
    private final Socket socket;
    private final User user;

    public ReceiveThread(Socket socket, User user) {
        this.socket = socket;
        this.user = user;
    }

    @Override
    public void run() {
        while (true) {
            Message<Resp<Object>> message = connectionUtil.receive(socket);
            Message.MessageType messageType = message.getType();
            if (messageType == Message.MessageType.LOGIN) login(message);
            else if (messageType == Message.MessageType.SHOW_USERS) showUserNames(message);
            else if (messageType == Message.MessageType.CHAT) chat(message);
            else if (messageType == Message.MessageType.CHAT_ALL) chat(message);
            else if (messageType == Message.MessageType.PUSH) receive(message);
            else if (messageType == Message.MessageType.FILE) receiveFile(message);
            else if (messageType == Message.MessageType.QUIT) {
                quit();
                break;
            }
        }
    }

    private void receiveFile(Message<Resp<Object>> message) {
        RespCode respCode = message.getData().getRespCode();
        if (respCode == RespCode.SUCCESS) {
            File file = (File) message.getData().getContent();
            String fileName = UUID.randomUUID().toString().substring(0, 6) + file.getSuffix();
            System.out.println(message.getSender() + " send content:" + message.getData().getContent());
            StreamUtil.writeToFile(fileName,file.getBytes());
        } else {
            System.out.println(respCode);
        }
    }

    private void quit() {
        user.setLoginStatus(false);
        SocketUtil.close(socket);
    }

    private void chat(Message<Resp<Object>> message) {
        RespCode respCode = message.getData().getRespCode();
        if (respCode == RespCode.SUCCESS) {
            System.out.println(message.getSender() + " send content:" + message.getData().getContent());
        } else {
            System.out.println(respCode);
        }
    }

    private void receive(Message<Resp<Object>> message) {
        RespCode respCode = message.getData().getRespCode();
        if (respCode == RespCode.SUCCESS) {
            System.out.println(message.getSender() + " send content:" + message.getData().getContent());
        } else {
            System.out.println(respCode);
        }
    }

    private void login(Message<Resp<Object>> message) {
        if (message.getData().getRespCode() == RespCode.SUCCESS) {
            user.setLoginStatus(true);
            return;
        }
        // fail
        System.out.println(message.getData().getRespCode());
        SocketUtil.close(socket);
    }

    public void showUserNames(Message<Resp<Object>> message) {
        if (message.getData().getRespCode() == RespCode.SUCCESS) {
            System.out.println(message.getData().getContent());
        } else System.out.println(message.getData().getRespCode());
    }
}
